/**
 * Copyright (c) 2007,中企动力华南研发中心<br>
 * All rights reserved.
 */
package com.ce.pms.charge.service.impl;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.ce.pms.base.service.impl.PmsBaseServiceImpl;
import com.ce.pms.charge.dao.ICharStoChoFixedDao;
import com.ce.pms.charge.dao.IChargeItemDao;
import com.ce.pms.charge.dao.IChargeStaDao;
import com.ce.pms.charge.dao.IPeriodLogDao;
import com.ce.pms.charge.model.AbstractStaVersion;
import com.ce.pms.charge.model.CharStaChoDomain;
import com.ce.pms.charge.model.ChargeItemDomain;
import com.ce.pms.charge.model.ChargeStaDomain;
import com.ce.pms.charge.service.ICharStoChoFixedService;
import com.ce.pms.charge.vo.CharStaChoFixedVO;
import com.ce.pms.charge.vo.ChargeStaChoVO;
import com.ce.pms.exception.BusinessException;
import com.ce.pms.house.service.IHouseService;
import com.ce.util.DateUtil;
import com.sitechasia.webx.core.support.Page;

/**
 * 功能描述：房间标准选用固定费用服务类<br>
 * @author 罗军林 创建日期：2008-8-7
 */
public class CharStoChoFixedServiceImpl extends PmsBaseServiceImpl implements
		ICharStoChoFixedService {
	ICharStoChoFixedDao charStoChoFixedDao;
	IChargeItemDao chargeItemDao;
	IChargeStaDao chargeStaDao;
	IHouseService houseService;
	IPeriodLogDao periodLogDao;

	/*
	 * (non-Javadoc)
	 * @see com.ce.pms.charge.service.ICharStoChoFixedService#queryCharStoChoFixed
	 * (com.ce.pms.charge.vo.ChargeStaChoConditionVO)
	 */
	public Page queryCharStoChoFixed(ChargeStaChoVO condition) {
		Page page=charStoChoFixedDao.queryCharStoCho(condition);

		page.setResult( convertDoToVoForList( page.getResult() ) );

		return page;
	}

	/*
	 * (non-Javadoc)
	 * @see com.ce.pms.charge.service.ICharStoChoFixedService#addCharStaChoFixed
	 * (com.ce.pms.charge.vo.CharStaChoFixedVO)
	 */
	public void addCharStaChoFixed(CharStaChoFixedVO charStaChoFixedVO){
		checkParameterIsNull(charStaChoFixedVO.getStartTime(),"charge:startTime_not_null");
		checkParameterIsNull(charStaChoFixedVO.getEndTime(),"charge:endTime_not_null");
		checkParameterIsNull(charStaChoFixedVO.getChargeStaID(), "charge:staid_not_null");
		checkParameterIsNull(charStaChoFixedVO.getChargeItemID(), "charge:itemid_not_null");
		checkCreatorInfo(charStaChoFixedVO.getCreater());
//		checkHouseIdsFormat(charStaChoFixedVO);
		ChargeStaDomain chargeStaDomain=chargeStaDao.findById(charStaChoFixedVO.getChargeStaID());
		charStaChoFixedVO.setChargeAmt(chargeStaDomain.getCVersion().getPrice());
		charStoChoFixedDao.addCharStaChoFixed(charStaChoFixedVO);
	}

	/**
	 * 功能描述:更新指定收费标准的房间标准选用表中的费用,如果该标准还没有被选用将返回false<br>
	 * @param charStaId<br>
	 * @param staVersion
	 * Create author:罗军林<br>
	 * Create on:2008-10-10<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	public boolean updateCharstChoAmt(Long charStaId,AbstractStaVersion staVersion){
		checkParameterIsNull(charStaId, "charge:itemid_not_null");
		if(null==staVersion){
			return false;
		}else{
			BigDecimal chargeAmt=staVersion.getPrice();
			charStoChoFixedDao.updateCharStoAmt(charStaId,chargeAmt);
			return true;
		}
	}

	/*
	 * (non-Javadoc)
	 * @see com.ce.pms.charge.service.ICharStoChoFixedService#deleteCharStaChoFixed
	 * (java.lang.Long)
	 */
	public void deleteCharStaChoFixed(Long keyword) {

		CharStaChoDomain charStaChoDomain=charStoChoFixedDao.findById(keyword);

		checkPeriodHasArcaculate(charStaChoDomain);

		charStoChoFixedDao.deleteObjectById(keyword);
	}

	/*
	 * (non-Javadoc)
	 * @see com.ce.pms.charge.service.ICharStoChoFixedService#editCharStaChoFixed
	 * (com.ce.pms.charge.vo.CharStaChoFixedVO)
	 */
	public CharStaChoFixedVO editCharStaChoFixed(CharStaChoFixedVO charStaChoFixedVO) {
		checkParameterIsNull(charStaChoFixedVO.getChargeStaID(), "charge:staid_not_null");
		checkParameterIsNull(charStaChoFixedVO.getChargeItemID(), "charge:itemid_not_null");

		ChargeItemDomain chargeItemDomain=chargeItemDao.findById(charStaChoFixedVO.getChargeItemID());
		ChargeStaDomain chargeStaDomain=chargeStaDao.findById(charStaChoFixedVO.getChargeStaID());
		CharStaChoDomain charStaChoDomain=charStoChoFixedDao.findById(charStaChoFixedVO.getKeyword());

		checkPeriodHasArcaculate(charStaChoDomain);

		Date startTime=DateUtil.convertStrToDate(DateUtil.getFirstDayOfMonthofYear( charStaChoFixedVO.getStartTime()),"yyyy-MM-dd");
		Date endTime=DateUtil.convertStrToDate(DateUtil.getLastDayOfMonthOfYear( charStaChoFixedVO.getEndTime()),"yyyy-MM-dd");

		charStaChoDomain.setChargeItem(chargeItemDomain);
		charStaChoDomain.setChargeSta(chargeStaDomain);
		charStaChoDomain.setChargeAmt(chargeStaDomain.getCVersion().getPrice());
		charStaChoDomain.setStartTime( startTime );
		charStaChoDomain.setEndTime( endTime );
		this.modifyInfo(charStaChoDomain.getSysInfo(), charStaChoFixedVO.getCreater());

		return convertDoToVO(charStaChoDomain);
	}

	/* (non-Javadoc)
	 * @see com.ce.pms.charge.service.ICharStoChoGeneralService#findCharStoChoById(java.lang.Long)
	 */
	public CharStaChoFixedVO findCharStoChoById(Long keyword) {
		this.checkParameterIsNull(keyword, "charge:staChoid_not_null");
		return convertDoToVO( charStoChoFixedDao.findById(keyword) );
	}

	/* (non-Javadoc)
	 * @see com.ce.pms.charge.service.ICharStoChoFixedService#checkExistCharStoChoFixed(com.ce.pms.charge.vo.CharStaChoFixedVO, com.ce.pms.charge.vo.CharStaChoFixedVO)
	 */
	public Boolean checkExistCharStoChoFixed(CharStaChoFixedVO searchCondition, CharStaChoFixedVO oldCondition) {
		return charStoChoFixedDao.checkExistCharStoChoFixed(searchCondition, oldCondition);
	}

	/**
	 * 功能描述:检查是否做过应收计算<br>
	 * @param charStaChoDomain<br>
	 * Create author:罗军林<br>
	 * Create on:2008-10-31<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	private void checkPeriodHasArcaculate(CharStaChoDomain charStaChoDomain) {
		if( periodLogDao.checkPeriodHasArcaculate(charStaChoDomain.getHouse().getBuilding().getKeyword()) ){
			throw new BusinessException("charge:peiord_has_calculate",BusinessException.ERROR);
		}
	}

	/** 功能描述:DO列表转VO列表<br>
	 * @param list<br>
	 * Create author:罗军林<br>
	 * Create on:2008-8-12<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	private List<CharStaChoFixedVO> convertDoToVoForList(List<CharStaChoDomain> list) {
		List<CharStaChoFixedVO> result=new ArrayList<CharStaChoFixedVO>();
		for(CharStaChoDomain charsta : list){
			CharStaChoFixedVO charStaChoFixedVO = convertDoToVO(charsta);
			result.add(charStaChoFixedVO);
		}
		return result;
	}

	/** 功能描述:DO转VO（走表）<br>
	 * @param charsta
	 * @return<br>
	 * Create author:罗军林<br>
	 * Create on:2008-8-12<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	private CharStaChoFixedVO convertDoToVO(CharStaChoDomain charsta) {
		CharStaChoFixedVO charStaChoFixedVO = new CharStaChoFixedVO();
		charStaChoFixedVO.setKeyword(charsta.getKeyword());
		if( charsta.getHouse() != null){
			charStaChoFixedVO.setHouseId(charsta.getHouse().getKeyword());
			charStaChoFixedVO.setHouseNumber(charsta.getHouse().getHouseNumber());
			charStaChoFixedVO.setOwnerName(houseService.getOwnerNameBy(charsta.getHouse().getHouseNumber()));
		}
		if( charsta.getChargeItem() != null){
			charStaChoFixedVO.setChargeItemID(charsta.getChargeItem().getKeyword());
			charStaChoFixedVO.setChargeItemName(charsta.getChargeItem().getItemName());
		}

		if(charsta.getChargeSta() != null){
			charStaChoFixedVO.setChargeStaID(charsta.getChargeSta().getKeyword());
			charStaChoFixedVO.setChargeStaName(charsta.getChargeSta().getStaName());
		}
		charStaChoFixedVO.setStartTime( getDateString("yyyy-MM", charsta.getStartTime()) );
		charStaChoFixedVO.setEndTime( getDateString("yyyy-MM", charsta.getEndTime()) );
		charStaChoFixedVO.setChargeAmt( charsta.getChargeAmt() );
		return charStaChoFixedVO;
	}

	/**
	 * 功能描述:获得指定格式的时间字符串，如果参数有一个为空，将返回null<br>
	 * @param pattern
	 * @param date
	 * @return<br>
	 * Create author:罗军林<br>
	 * Create on:2008-8-14<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	private String getDateString(String pattern,Date date){
		if(pattern==null || date==null){
			return null;
		}else{
			 SimpleDateFormat sf=new SimpleDateFormat(pattern);
			 return sf.format(date);
		}
	}
	/**
	 * 功能描述:检查房号列表是否符合指定格式，例1,2,3,4<br>
	 * @param charStaChoFixedVO<br>
	 * Create author:罗军林<br>
	 * Create on:2008-8-13<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	private void checkHouseIdsFormat(CharStaChoFixedVO charStaChoFixedVO) {
		if( charStaChoFixedVO.getHouseIds() == null ||
				!charStaChoFixedVO.getHouseIds().matches("((\\d,)|(\\d))+?")){
			throw new BusinessException("charge:houseid_format_exception",BusinessException.WARNING);
		}
	}


	/**********************************************************************************/
	public void setChargeItemDao(IChargeItemDao chargeItemDao) {
		this.chargeItemDao = chargeItemDao;
	}

	public void setChargeStaDao(IChargeStaDao chargeStaDao) {
		this.chargeStaDao = chargeStaDao;
	}

	public void setCharStoChoFixedDao(ICharStoChoFixedDao charStoChoFixedDao) {
		this.charStoChoFixedDao = charStoChoFixedDao;
	}

	public void setHouseService(IHouseService houseService) {
		this.houseService = houseService;
	}

	public void setPeriodLogDao(IPeriodLogDao periodLogDao) {
		this.periodLogDao = periodLogDao;
	}

}
